/**
 *
 */
package utils;

import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import mainApp.HighScore;
import mainApp.Word;

/**
 * @author Picto
 *
 */
public class Utils {

    public Utils() {
    }

    public ArrayList<Character> truncateCharArrayList(ArrayList<Character> al, int length) {
        for (int i = 0; i < al.size(); i++) {
            if (i > length - 1) {
                al.remove(i);
            }
        }
        al.trimToSize();
        return al;
    }

    public ArrayList<HighScore> truncateHighScoreArrayList(ArrayList<HighScore> al) {
        for (int i = 0; i < al.size(); i++) {
            if (i > 9) {
                al.remove(i);
            }
        }
        al.trimToSize();
        return al;
    }
    
    public void checkForHighScoreFile() {
        if (new File("HighScores.txt").exists() == false) {
            try {
                File hs = new File("HighScores.txt");
                hs.createNewFile();
                
                for (int i = 0; i < 10; i++) {
                    try { 
                        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("HighScores.txt", true)));
                        out.write("notSet");
                        out.write(System.getProperty("line.separator"));
                        out.write("0");
                        out.write(System.getProperty("line.separator"));
                        out.close();
                    } catch (FileNotFoundException e) {
                        System.out.println("File not found.");
                    } catch (IOException e) {
                        System.out.println("File cant be read.");
                    }
                }
            } catch (IOException ex) {
                System.out.println("Failed to create file");
            }
        }
    }

    public String getWordString(Word word, ArrayList lettersAttempted) {
        String wordStr = "";
        for (int i = 0; i < word.getWordLength(); i++) {
            if (lettersAttempted.get(i).equals(' ')) {
                wordStr += ("_");
            } else {
                wordStr += (lettersAttempted.get(i));
            }

        }
        return wordStr;
    }

    public char getLetterClicked(MouseEvent e) {
        char letterClicked = ' ';
        for (int iY = 199; iY < 319; iY += 40) {
            for (int iX = 595; iX < 846; iX += 36) {
                if (e.getX() > iX && e.getX() < iX + 36 && e.getY() > iY && e.getY() < iY + 40) {
                    if (iX == 595 && iY == 199) {
                        letterClicked = 'a';
                    } else if (iX == 631 && iY == 199) {
                        letterClicked = 'b';
                    } else if (iX == 667 && iY == 199) {
                        letterClicked = 'c';
                    } else if (iX == 703 && iY == 199) {
                        letterClicked = 'd';
                    } else if (iX == 739 && iY == 199) {
                        letterClicked = 'e';
                    } else if (iX == 775 && iY == 199) {
                        letterClicked = 'f';
                    } else if (iX == 811 && iY == 199) {
                        letterClicked = 'g';
                    } else if (iX == 595 && iY == 239) {
                        letterClicked = 'h';
                    } else if (iX == 631 && iY == 239) {
                        letterClicked = 'i';
                    } else if (iX == 667 && iY == 239) {
                        letterClicked = 'j';
                    } else if (iX == 703 && iY == 239) {
                        letterClicked = 'k';
                    } else if (iX == 739 && iY == 239) {
                        letterClicked = 'l';
                    } else if (iX == 775 && iY == 239) {
                        letterClicked = 'm';
                    } else if (iX == 811 && iY == 239) {
                        letterClicked = 'n';
                    } else if (iX == 595 && iY == 279) {
                        letterClicked = 'o';
                    } else if (iX == 631 && iY == 279) {
                        letterClicked = 'p';
                    } else if (iX == 667 && iY == 279) {
                        letterClicked = 'q';
                    } else if (iX == 703 && iY == 279) {
                        letterClicked = 'r';
                    } else if (iX == 739 && iY == 279) {
                        letterClicked = 's';
                    } else if (iX == 775 && iY == 279) {
                        letterClicked = 't';
                    } else if (iX == 811 && iY == 279) {
                        letterClicked = 'u';
                    } else {
                        letterClicked = ' ';
                    }
                }
            }
        }
        for (int iX = 595; iX < 846; iX += 36) {
            if (e.getX() > iX && e.getX() < iX + 36 && e.getY() > 319 && e.getY() < 359) {
                switch(iX){
                    case 595: letterClicked = 'v'; break;
                    case 631: letterClicked = 'w'; break;
                    case 667: letterClicked = 'x'; break;
                    case 703: letterClicked = 'y'; break;
                    case 739: letterClicked = 'z'; break;
                }
            }
        }
        return letterClicked;
    }
}
